package com.enqualcomm.kids.config.userdefault;


import com.enqualcomm.kids.component.MyApplication;
import com.enqualcomm.kids.network.socket.response.AllTerminalConfig;
import com.enqualcomm.kids.network.socket.response.HeartRateModeQueryResult;
import com.enqualcomm.kids.network.socket.response.HeartRateQueryResult;
import com.enqualcomm.kids.network.socket.response.QueryStepDayListResult;
import com.enqualcomm.kids.network.socket.response.QueryStepHourListResult;
import com.enqualcomm.kids.network.socket.response.TerminalConfigResult;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import common.utils.BufferedSharedPreferences;
import common.utils.GsonFactory;

/**
 * Created by yinzhiqun on 2015/9/1.
 */
public class TerminalDefault {

    private final BufferedSharedPreferences sp;
    private final String terminalid;

    public TerminalDefault(String terminalid) {
        this.terminalid = terminalid;
        sp = MyApplication.getInstance().getSp(terminalid + "_t");
    }

    public String getTerminalid() {
        return terminalid;
    }

    public void setNewConfig(AllTerminalConfig.Config config) {
        CacheEntry.set(Keys.Terminal.NEW_CONFIG + terminalid, config);
        if (config == null) {
            String s = GsonFactory.newInstance().toJson(config);
            sp.saveString(Keys.Terminal.NEW_CONFIG, s);
        } else {
            sp.remove(Keys.Terminal.NEW_CONFIG);
        }
    }


    public AllTerminalConfig.Config getNewConfig() {
        AllTerminalConfig.Config config = (AllTerminalConfig.Config) CacheEntry.get(Keys.Terminal.NEW_CONFIG + terminalid);
        if (config != null) {
            return config;
        } else {
            String s = sp.getString(Keys.Terminal.NEW_CONFIG, null);
            if (s != null) {
                config = GsonFactory.newInstance().fromJson(s, AllTerminalConfig.Config.class);
                CacheEntry.set(Keys.Terminal.CONFIG + terminalid, config);
                return config;
            }
            return null;
        }
    }

    public void setConfig(TerminalConfigResult.Data config) {
        CacheEntry.set(Keys.Terminal.CONFIG + terminalid, config);

        if (config != null) {
            String s = GsonFactory.newInstance().toJson(config);
            sp.saveString(Keys.Terminal.CONFIG, s);
        } else {
            sp.remove(Keys.Terminal.CONFIG);
        }
    }

    public TerminalConfigResult.Data getConfig() {
        TerminalConfigResult.Data obj = (TerminalConfigResult.Data) CacheEntry.get(Keys.Terminal.CONFIG + terminalid);

        if (obj != null) {
            return obj;
        } else {
            String s = sp.getString(Keys.Terminal.CONFIG, null);
            if (s != null) {
                obj = GsonFactory.newInstance().fromJson(s, TerminalConfigResult.Data.class);
                CacheEntry.set(Keys.Terminal.CONFIG + terminalid, obj);
                return obj;
            }
            return null;
        }
    }

    public void setHeartRateSettings(HeartRateModeQueryResult.Result config) {
        CacheEntry.set(Keys.Terminal.HEART_RATE_SETTING + terminalid, config);

        if (config != null) {
            String s = GsonFactory.newInstance().toJson(config);
            sp.saveString(Keys.Terminal.HEART_RATE_SETTING, s);
        } else {
            sp.remove(Keys.Terminal.HEART_RATE_SETTING);
        }
    }

    public HeartRateModeQueryResult.Result getHeartRateSettings() {
        HeartRateModeQueryResult.Result obj = (HeartRateModeQueryResult.Result) CacheEntry.get(Keys.Terminal.HEART_RATE_SETTING + terminalid);

        if (obj != null) {
            return obj;
        } else {
            String s = sp.getString(Keys.Terminal.HEART_RATE_SETTING, null);
            if (s != null) {
                obj = GsonFactory.newInstance().fromJson(s, HeartRateModeQueryResult.Result.class);
                CacheEntry.set(Keys.Terminal.HEART_RATE_SETTING + terminalid, obj);
                return obj;
            }
            return null;
        }
    }

    public void setHeartRateList(HeartRateQueryResult config) {
        CacheEntry.set(Keys.Terminal.HEART_RATE_LIST + terminalid, config);

        if (config != null) {
            String s = GsonFactory.newInstance().toJson(config);
            sp.saveString(Keys.Terminal.HEART_RATE_LIST, s);
        } else {
            sp.remove(Keys.Terminal.HEART_RATE_LIST);
        }
    }

    public List<HeartRateQueryResult.Result> getHeartRateList() {
        HeartRateQueryResult obj = (HeartRateQueryResult) CacheEntry.get(Keys.Terminal.HEART_RATE_LIST + terminalid);

        if (obj != null) {
            return obj.result;
        } else {
            String s = sp.getString(Keys.Terminal.HEART_RATE_LIST, null);
            if (s != null) {
                obj = GsonFactory.newInstance().fromJson(s, HeartRateQueryResult.class);
                CacheEntry.set(Keys.Terminal.HEART_RATE_LIST + terminalid, obj);
                return obj.result;
            }
            return null;
        }
    }

    public QueryStepHourListResult.Data getStepHourList() {
        QueryStepHourListResult.Data obj = (QueryStepHourListResult.Data) CacheEntry.get(Keys.Terminal.HOUR_LIST + terminalid);

        if (obj != null) {
            return obj;
        } else {
            String s = sp.getString(Keys.Terminal.HOUR_LIST, null);
            if (s != null) {
                obj = GsonFactory.newInstance().fromJson(s, QueryStepHourListResult.Data.class);
                CacheEntry.set(Keys.Terminal.HOUR_LIST + terminalid, obj);
                return obj;
            }
            return null;
        }
    }

    public void setStepHourList(QueryStepHourListResult.Data data) {
        CacheEntry.set(Keys.Terminal.HOUR_LIST + terminalid, data);

        if (data != null) {
            String s = GsonFactory.newInstance().toJson(data);
            sp.saveString(Keys.Terminal.HOUR_LIST, s);
        } else {
            sp.remove(Keys.Terminal.HOUR_LIST);
        }
    }

    public void setStepDayList(QueryStepDayListResult.Data data) {
        CacheEntry.set(Keys.Terminal.DAY_LIST + terminalid, data);

        if (data != null) {
            String s = GsonFactory.newInstance().toJson(data);
            sp.saveString(Keys.Terminal.DAY_LIST, s);
        } else {
            sp.remove(Keys.Terminal.DAY_LIST);
        }
    }

    public QueryStepDayListResult.Data getStepDayList() {
        QueryStepDayListResult.Data obj = (QueryStepDayListResult.Data) CacheEntry.get(Keys.Terminal.DAY_LIST + terminalid);

        if (obj != null) {
            return obj;
        } else {
            String s = sp.getString(Keys.Terminal.DAY_LIST, null);
            if (s != null) {
                obj = GsonFactory.newInstance().fromJson(s, QueryStepDayListResult.Data.class);
                CacheEntry.set(Keys.Terminal.DAY_LIST + terminalid, obj);
                return obj;
            }
            return null;
        }
    }


    public void setSilenceCount(String count) {
        CacheEntry.set(Keys.Terminal.SILENCE_COUNT + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.SILENCE_COUNT, count);
        } else {
            sp.remove(Keys.Terminal.SILENCE_COUNT);
        }
    }

    public String getSilenceCount() {
        String count = (String) CacheEntry.get(Keys.Terminal.SILENCE_COUNT + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.SILENCE_COUNT, null);
            CacheEntry.set(Keys.Terminal.SILENCE_COUNT + terminalid, count);
        }
        return count;
    }

//    public void setDetachNoticeCount(String count){
//        CacheEntry.set(Keys.Terminal.NOTICE_COUNT+terminalid,count);
//
//        if (count!=null){
//            sp.saveString(Keys.Terminal.NOTICE_COUNT,count);
//        }else{
//            sp.remove(Keys.Terminal.NOTICE_COUNT);
//        }
//    }
//
//    public String getDetachNoticeCount(){
//        String count = (String) CacheEntry.get(Keys.Terminal.NOTICE_COUNT+terminalid);
//        if (count==null){
//            count = sp.getString(Keys.Terminal.NOTICE_COUNT, null);
//            CacheEntry.set(Keys.Terminal.NOTICE_COUNT+terminalid, count);
//        }
//        return count;
//    }

    public void setAlarmCount(String count) {
        CacheEntry.set(Keys.Terminal.ALARM_COUNT + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.ALARM_COUNT, count);
        } else {
            sp.remove(Keys.Terminal.ALARM_COUNT);
        }
    }

    public String getAlarmCount() {
        String count = (String) CacheEntry.get(Keys.Terminal.ALARM_COUNT + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.ALARM_COUNT, null);
            CacheEntry.set(Keys.Terminal.ALARM_COUNT + terminalid, count);
        }
        return count;
    }

    public void setFencingCount(String count) {
        CacheEntry.set(Keys.Terminal.FENCING_COUNT + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.FENCING_COUNT, count);
        } else {
            sp.remove(Keys.Terminal.FENCING_COUNT);
        }
    }

    public String getFencingCount() {
        String count = (String) CacheEntry.get(Keys.Terminal.FENCING_COUNT + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.FENCING_COUNT, null);
            CacheEntry.set(Keys.Terminal.FENCING_COUNT + terminalid, count);
        }
        return count;
    }

    public void setLat(String count) {
        CacheEntry.set(Keys.Terminal.LAT + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.LAT, count);
        } else {
            sp.remove(Keys.Terminal.LAT);
        }
    }

    public String getLat() {
        String count = (String) CacheEntry.get(Keys.Terminal.LAT + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.LAT, null);
            CacheEntry.set(Keys.Terminal.LAT + terminalid, count);
        }
        return count;
    }

    public void setLng(String count) {
        CacheEntry.set(Keys.Terminal.LNG + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.LNG, count);
        } else {
            sp.remove(Keys.Terminal.LNG);
        }
    }

    public String getLng() {
        String count = (String) CacheEntry.get(Keys.Terminal.LNG + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.LNG, null);
            CacheEntry.set(Keys.Terminal.LNG + terminalid, count);
        }
        return count;
    }


    public void setCityName(String count) {
        CacheEntry.set(Keys.Terminal.CITYNAME + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.CITYNAME, count);
        } else {
            sp.remove(Keys.Terminal.CITYNAME);
        }
    }


    public String getCityName() {
        String count = (String) CacheEntry.get(Keys.Terminal.CITYNAME + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.CITYNAME, null);
            CacheEntry.set(Keys.Terminal.CITYNAME + terminalid, count);
        }
        return count;
    }

    public void setTemp(String count) {
        CacheEntry.set(Keys.Terminal.TEMP + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.TEMP, count);
        } else {
            sp.remove(Keys.Terminal.TEMP);
        }
    }

    public String getTemp() {
        String count = (String) CacheEntry.get(Keys.Terminal.TEMP + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.TEMP, null);
            CacheEntry.set(Keys.Terminal.TEMP + terminalid, count);
        }
        return count;
    }

    public void setHighTemp(String count) {
        CacheEntry.set(Keys.Terminal.HIGHTEMP + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.HIGHTEMP, count);
        } else {
            sp.remove(Keys.Terminal.HIGHTEMP);
        }
    }

    public String getHighTemp() {
        String count = (String) CacheEntry.get(Keys.Terminal.HIGHTEMP + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.HIGHTEMP, null);
            CacheEntry.set(Keys.Terminal.HIGHTEMP + terminalid, count);
        }
        return count;
    }

    public void setLowTemp(String count) {
        CacheEntry.set(Keys.Terminal.LOWTEMP + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.LOWTEMP, count);
        } else {
            sp.remove(Keys.Terminal.LOWTEMP);
        }
    }

    public String getLowTemp() {
        String count = (String) CacheEntry.get(Keys.Terminal.LOWTEMP + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.LOWTEMP, null);
            CacheEntry.set(Keys.Terminal.LOWTEMP + terminalid, count);
        }
        return count;
    }

    public void setWeatherType(String count) {
        CacheEntry.set(Keys.Terminal.WEATHERTYPE + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.WEATHERTYPE, count);
        } else {
            sp.remove(Keys.Terminal.WEATHERTYPE);
        }
    }

    public String getWeatherType() {
        String count = (String) CacheEntry.get(Keys.Terminal.WEATHERTYPE + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.WEATHERTYPE, null);
            CacheEntry.set(Keys.Terminal.WEATHERTYPE + terminalid, count);
        }
        return count;
    }

    public void setRecentWeather(String count) {
        CacheEntry.set(Keys.Terminal.RECENTWEATHER + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.RECENTWEATHER, count);
        } else {
            sp.remove(Keys.Terminal.RECENTWEATHER);
        }
    }

    public String getRecentWeather() {
        String count = (String) CacheEntry.get(Keys.Terminal.RECENTWEATHER + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.RECENTWEATHER, null);
            CacheEntry.set(Keys.Terminal.RECENTWEATHER + terminalid, count);
        }
        return count;
    }

    public void setStepsToday(String count) {
        CacheEntry.set(Keys.Terminal.TODATSTEPS + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.TODATSTEPS, count);
        } else {
            sp.remove(Keys.Terminal.TODATSTEPS);
        }
    }

    public String getStepsToday() {
        String count = (String) CacheEntry.get(Keys.Terminal.TODATSTEPS + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.TODATSTEPS, null);
            CacheEntry.set(Keys.Terminal.TODATSTEPS + terminalid, count);
        }
        return count;
    }

    public List<String> getFencingIds() {
        ArrayList<String> ids = new ArrayList<>();
        String str = (String) CacheEntry.get(Keys.Terminal.FENCING_IDS + terminalid);
        if (str == null) {
            str = sp.getString(Keys.Terminal.FENCING_IDS, null);
            CacheEntry.set(Keys.Terminal.FENCING_IDS + terminalid, str);
        }
        if (str != null) {
            String[] split = str.split("#");
            for (int i = 0; i < split.length; i++) {
                ids.add(split[i]);
            }
        }
        return ids;
    }

    public void setFencingIds(List<String> ids) {
        String string = null;
        if (ids == null || ids.isEmpty()) {
            string = "";
        } else {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < ids.size(); i++) {
                builder.append("#").append(ids.get(i));
            }
            string = builder.deleteCharAt(0).toString();
        }

        CacheEntry.set(Keys.Terminal.FENCING_IDS + terminalid, string);
        sp.saveString(Keys.Terminal.FENCING_IDS, string);
    }

    public void setHFForecastWeather(String count) {
        CacheEntry.set(Keys.Terminal.HFRECENTWEATHER + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.HFRECENTWEATHER, count);
        } else {
            sp.remove(Keys.Terminal.HFRECENTWEATHER);
        }
    }

    public String getHFForecastWeather() {
        String count = (String) CacheEntry.get(Keys.Terminal.HFRECENTWEATHER + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.HFRECENTWEATHER, null);
            CacheEntry.set(Keys.Terminal.HFRECENTWEATHER + terminalid, count);
        }
        return count;
    }

    public void setHFNowWeather(String count) {
        CacheEntry.set(Keys.Terminal.HFNOWWEATHER + terminalid, count);

        if (count != null) {
            sp.saveString(Keys.Terminal.HFNOWWEATHER, count);
        } else {
            sp.remove(Keys.Terminal.HFNOWWEATHER);
        }
    }

    public String getHFNowWeather() {
        String count = (String) CacheEntry.get(Keys.Terminal.HFNOWWEATHER + terminalid);
        if (count == null) {
            count = sp.getString(Keys.Terminal.HFNOWWEATHER, null);
            CacheEntry.set(Keys.Terminal.HFNOWWEATHER + terminalid, count);
        }
        return count;
    }

}
